package net.zabuchy.project.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;

import net.zabuchy.iteration.entity.Iteration;
import net.zabuchy.user.entity.*;

@Entity
@Table(name="PROJECT", catalog="quickstart")
public class Project implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private Integer projectId;
	private Integer customerId;
	private String title;
	private String description;
	private String status;
	private Integer defaultIterationLenght;
	private Integer nextIterationLenght;
	private Integer currentIterationNumber;
	private Date creationdate;
	private Date predictedEndDate;
	private Date startDate;
	
	private Set<User> users = new HashSet<User>(0);
	private Set<Iteration> iterations = new HashSet<Iteration>(0);
	
	public Project() {
	}
	
	public Project(Integer customerId, String title, 
			String description, Set<User> users ) {
		this.customerId = customerId;
		this.title = title;
		this.description = description;
		this.users = users;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="PROJECT_ID", unique=true, nullable = false)
	public Integer getProjectId() {
		return projectId;
	}
	public void setProjectId(Integer projectID) {
		this.projectId = projectID;
	}
	
	@Column(name="CUSTOMER_ID", nullable = false, length=10)
	public Integer getCustomerId() {
		return customerId;
	}
	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}
	
	@Column(name = "TITLE", nullable = false)
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	@Column(name = "DESCRIPTION", nullable = false)
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	@Column(name = "DEFAULT_ITERATION_LENGHT", nullable = false)
	public Integer getDefaultIterationLenght() {
		return defaultIterationLenght;
	}
	public void setDefaultIterationLenght(Integer defaultIterationLenght) {
		this.defaultIterationLenght = defaultIterationLenght;
	}

	@Column(name = "STATUS", nullable = false)
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@Column(name = "NEXT_ITERATION_LENGHT", nullable = false)
	public Integer getNextIterationLenght() {
		return nextIterationLenght;
	}

	public void setNextIterationLenght(Integer nextIterationLenght) {
		this.nextIterationLenght = nextIterationLenght;
	}

	@Column(name = "CURRENT_ITERATION_NUMBER", nullable = false)
	public Integer getCurrentIterationNumber() {
		return currentIterationNumber;
	}

	public void setCurrentIterationNumber(Integer currentIterationNumber) {
		this.currentIterationNumber = currentIterationNumber;
	}

	@Column(name = "CREATION_DATE", nullable = false)
	public Date getCreationdate() {
		return creationdate;
	}

	public void setCreationdate(Date creationdate) {
		this.creationdate = creationdate;
	}

	@Column(name = "PREDICTED_END_TIME", nullable = false)
	public Date getPredictedEndDate() {
		return predictedEndDate;
	}

	public void setPredictedEndDate(Date predictedEndDate) {
		this.predictedEndDate = predictedEndDate;
	}

	@ManyToMany(fetch=FetchType.EAGER)
	@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
	@JoinTable(name="USER_PROJECTS", catalog="quickstart", joinColumns={
			@JoinColumn(name="PROJECT_ID", nullable=false, updatable=false)},
			inverseJoinColumns = { @JoinColumn(name="USER_ID", nullable=false, updatable=false)})
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "project")
	public Set<Iteration> getIterations() {
		return iterations;
	}
	
	public void setIterations(Set<Iteration> iterations) {
		this.iterations = iterations;
	}

	@Column(name = "START_DATE")
	public Date getStartDate() {
		return startDate;
	}
	
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	
	@Override
	public String toString() {
		return "Project [projectId=" + projectId + ", customerId=" + customerId
				+ ", title=" + title + ", description=" + description
				+ ", defaultIterationLenght=" + defaultIterationLenght
				+ ", users=" + users + ", iterations=" + iterations + "]";
	}

	
}

